package mBio;

import java.io.*;
import java.util.*;
import rfBio.*;

public class psl3_bestkeeper{

	public static void main(String[] args)
		throws IOException, FileNotFoundException{
			
		if(args.length != 3){
			System.out.println("Wrong number of parameters!" + '\n' + "Usage: psl3_bestkeeper sorted_psl3_file out_psl3_file min_score_to_include.");
			System.exit(0);
		}

		// local variables
		Vector temp_psl3vector = new Vector(100,100); 
		boolean addFlag = true;
		boolean first = true;
		psl3 best_psl3 = new psl3();
		String currQname = "";
		


		BufferedReader refFile = new BufferedReader ( new FileReader (args[0]));
		PrintWriter o_file = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
		
		// read sorted psl3 file line by line, store in ref_vector
		while (true){
			String line = refFile.readLine();
			
			// break if reached end of file
			if (line == null)
			{
				break;
			}
			
			psl3 temp_psl3 = new psl3(line);

			// set curr Q_name to first accession in file
			if(first){
				currQname = temp_psl3.get_Q_name();
				best_psl3.set(temp_psl3);
				first = false;
			}
			
			//if new sequence, print best from previous to outfile
			if(!currQname.equals(temp_psl3.get_Q_name())){
				o_file.write(best_psl3.export());
				currQname = temp_psl3.get_Q_name();
				best_psl3.set(temp_psl3);
			}
			
			if(temp_psl3.get_score() > best_psl3.get_score()){
				best_psl3.set(temp_psl3);
			}
		
		} 		

		// close files
		refFile.close();
		o_file.close();	
   }
}